/**
 * @file	at_command.h
 * @author	chensj0621
 * @brief	
 * @version	0.1
 * @date	2021-11-11
 * @copyright Copyright (c) 2020, CHIPSEA Co., Ltd.
 * @note
 */
#ifndef AT_COMMAND_H
#define AT_COMMAND_H

#ifdef __cplusplus
extern "C"
{
#endif

#include <string.h>
#include "sdk_config.h"
#include "log.h"
#include "key.h"
#include "OSAL.h"
#include "gpio.h"
#include "flash.h"
#include "ll.h"
#include "hci.h"
#include "ll_def.h"
#include "uart.h"
#include "pwrmgr.h"
#include "gap.h"
#include "multi.h"
#include "hal_uart.h"
#include "multi_role.h"
#include "gap_internal.h"
#include "rf_phy_driver.h"

#define AT_VERSION       136//版本号

#define AT_CMD_FIND      0x7F//表示查询动作
#define AT_CMD_ERROR     0xFF//表示非法动作
#define AT_CMD_SUCCES    0x80//成功
#define AT_NULL_BRIDGE   0x81//桥接不存在



typedef void (*at_func)(uint8_t* p,uint8_t len);


typedef struct
{
    uint8_t *at;                //AT命令
    uint8_t at_len;             //AT命令长度
    uint8_t ble_enable;         //AT命令蓝牙通道是否使能
    uint8_t must_param_num;     //AT命令允许最大必选参数
    uint8_t optional_param_num; //AT命令允许最大可选参数
    void *cb;                   //回调指针

}at_cmd_handle_t;


typedef struct
{
    uint8_t sacn_time;//扫描设定时间
    uint8_t file_name[18];//过滤的蓝牙名
    uint8_t ok_flag;//收到ok的标志位
    uint8_t data_type;//at命令数据类型
    uint8_t data_type_index;//at命令数据通道
    uint8_t ble_data_fifo[247];//ble数据缓存
    uint8_t ble_data_len;
    uint8_t ble_data_index;//ble数据角色的句柄
    uint8_t ble_data_role;//ble数据的角色
    uint8_t ble_send_index;//ble发送数据方向
    uint8_t ble_send_role;//ble发送数据角色
    //at_bridge_t bridge[AT_BRIDGE_NUM];
    //uint8_t bridge_channel;
    uint8_t at_cmd_flag;
}at_cm_t;



typedef enum 
{
    AT_BLE = 0,//ble数据
    AT_UART ,  //uart数据
    AT_MASTER, //主机
    AT_SLAVE,  //从主
}at_data_type_e;

typedef enum 
{
    BLE_ONLY_SLAVE = 0,//单从模式
    BLE_ONLY_MASTER ,  //主机模式
    BLE_MASTER_AND_SLAVE,//多主多从模式
}ble_at_mode_e;

extern at_cm_t at_data;

void at_rst(uint8_t *data,uint8_t len);
void at_baud(uint8_t *data,uint8_t len);
void at_mac(uint8_t *data,uint8_t len);
void at_adven(uint8_t *data,uint8_t len);
void at_advin(uint8_t *data,uint8_t len);
void at_connin(uint8_t *data,uint8_t len);
void at_ver(uint8_t *data,uint8_t len);
void at_scan(uint8_t *data,uint8_t len);
void at_role(uint8_t *data,uint8_t len);
void at_connlist(uint8_t *data,uint8_t len);
void at_cancel_scanen(uint8_t *data,uint8_t len);
void at_clrfile(uint8_t *data,uint8_t len);
void at_setfile(uint8_t *data,uint8_t len);
void at_restore(uint8_t *data,uint8_t len);
void at_name(uint8_t *data,uint8_t len);
void at_ota(uint8_t *data,uint8_t len);
void at_command_process(uint8_t *data, uint8_t len, uint8_t type);
void at_link(uint8_t *data,uint8_t len);
void at_getstat(uint8_t *data,uint8_t len);
void at_disc(uint8_t *data,uint8_t len);
void at_power(uint8_t *data,uint8_t len);
void reply_at_data(uint8_t *str ,uint8_t len, uint8_t *data,uint8_t data_len);
void reply_ok(uint8_t type);
void at_constat(void);
void at_sleep(uint8_t *data,uint8_t len);
void at_bridge(uint8_t *data,uint8_t len);
void at_clrlink(uint8_t *data,uint8_t len);
void at_service(uint8_t *data,uint8_t len);
void at_txhandle(uint8_t *data,uint8_t len);
void at_service(uint8_t *data,uint8_t len);
void at_autolink(uint8_t *data,uint8_t len);
void at_rxhandle(uint8_t role ,uint8_t rxhandle);
uint8_t ble_get_ok_flag(void);
void at_autolinkcfg(uint8_t *data,uint8_t len);
void at_clrbridge(uint8_t *data,uint8_t len);
uint8_t ble_set_ok_flag(uint8_t flag);
uint8_t get_at_cmd_error(uint8_t *data, uint8_t len,const at_cmd_handle_t *at,uint8_t idx);
uint8_t get_at_data_1byte(uint8_t *data,uint8_t len);
void at_white_list_enable(uint8_t channel,uint8_t enable);
void at_rssi(uint8_t *data,uint8_t len);
void at_clrrssi(uint8_t *data,uint8_t len);
/********/
void ble_set_at_mode(uint8_t at_mode);
uint8_t ble_get_at_mode(void);
uint8_t at_bridge_deal(uint8_t role,uint8_t index,attMsg_t *sendMsg);
void at_send_data(uint8_t type,uint8_t *data,uint8_t len);
#ifdef __cplusplus
}
#endif

#endif /* AT_COMMAND_H */
